package com.hp.attendance.serlvet.empmanage.lizhi;

import com.hp.attendance.entity.Department;
import com.hp.attendance.entity.History;
import com.hp.attendance.entity.PageInfo;
import com.hp.attendance.entity.Position;
import com.hp.attendance.entity.dto.HisDepPosDto;
import com.hp.attendance.service.DepartmentService;
import com.hp.attendance.service.HistoryService;
import com.hp.attendance.service.PositionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//查询离休员工列表
@WebServlet("/empMangeLZList")
public class EmpManageLXpageServlet extends HttpServlet {
    private HistoryService historyService = new HistoryService();
    private DepartmentService departmentService = new DepartmentService();
    private PositionService positionService = new PositionService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //分页查询
        try {
            String pageNo = req.getParameter("pageNo");
            if(pageNo==null ||"".equals(pageNo.trim())){
                pageNo="1";
            }
            PageInfo<History> page = historyService.getPage(Integer.parseInt(pageNo), 10);
            List<Department> list = departmentService.getList();
            List<Position> list1 = positionService.getList();
            ArrayList<HisDepPosDto> empDepPosDtoArrayList = new ArrayList<HisDepPosDto>();
            for (History record : page.getRecords()) {
                if (record.getStatus().equals("离职") ||record.getStatus().equals("退休")){
                    HisDepPosDto empDepPosDto = new HisDepPosDto();
                    empDepPosDto.setHistory(record);
                    for (Department department : list) {
                        if (department.getDepartment_number().equals(record.getDepartment_number())){
                            empDepPosDto.setDepartment_name(department.getName());
                        }
                    }
                    for (Position position : list1) {
                        if (position.getPosition_number().equals(record.getPosition_number())){
                            empDepPosDto.setPosition_name(position.getName());
                        }
                    }
                    empDepPosDtoArrayList.add(empDepPosDto);
                }
            }
            PageInfo<HisDepPosDto> page1 = new PageInfo<HisDepPosDto>();
            page1.setRecords(empDepPosDtoArrayList);
            page1.setPages(page.getPages());
            page1.setCurrent(page.getCurrent());
            page1.setTotal(page.getTotal());
            req.setAttribute("page",page1);
            req.getRequestDispatcher("/pages/history_list_2.jsp").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
